<%@ page import="org.apache.ibatis.session.SqlSession" %>
<%@ page import="com.cn.Mapper.userMapper.UserMapper" %>
<%@ page import="com.cn.util.SqlsesstionUtil" %>
<%@ page import="com.cn.entity.user.User" %>
<%@ page import="java.util.List" %><%--
  Created by IntelliJ IDEA.
  User: yujiajing
  Date: 2022/10/3
  Time: 14:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <title>使用者表</title>
<%--  引入jQuery包  --%>
  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.js"></script>

    <!-- 引入layui css -->
    <link rel="stylesheet" type="text/css" href="https://www.layuicdn.com/layui-v2.5.6/css/layui.css" />

    <!-- 引入layui js -->
    <script src="https://www.layuicdn.com/layui-v2.5.6/layui.js"></script>

</head>

<body>

<script>
    function ConfirmDelete(id){
        if(confirm("确认删除数据？")){
            OperatorUser(id);
        }
    }

    function OperatorUser(id) {

        $.ajax({

            type : "post",

            url : "user/deleteById",

            dateType : "String",

            data : {

                id : id

            },

            success : function(data) {

                $("#tr_"+id.substring(4)).remove();

             alert("操作成功！")

            },

            error : function() {

                alert("网络异常，请稍后重试");

            }

        });

    }
</script>

<%
  SqlSession sqlSession = SqlsesstionUtil.getSqlSession();
  UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
  List<User> userLinkedList = userMapper.selectAllUser();
%>
<table id="UserTable" class="layui-table"  >

    <tr>
        <td>id</td>
    <td>username</td>
    <td>password</td>
    <td >operate</td>
    </tr>

  <%for (User user:userLinkedList){%>
<%--  --%>
    <tr id="tr_<%=user.getId()%>">
<%--    --%>
    <td><%=user.getId()%></td>
    <td><%=user.getUsername()%></td>
    <td><%=user.getPassword()%></td>
<%--    <td><a href="UpdateUser.jsp?id=<%=user.getId()%>&username=<%=user.getUsername()%>&password=<%=user.getPassword()%>">修改</a> </td>--%>

      <%--  <button class="layui-btn layui-btn-danger layui-btn-radius" id="del_<%=user.getId()%>" value="删除"

                onclick="ConfirmDelete(this.id)">删除</button>--%>
<td>
    <a class="layui-btn layui-btn-xs" lay-event="edit" href="UpdateUser.jsp?id=<%=user.getId()%>&username=<%=user.getUsername()%>&password=<%=user.getPassword()%>">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs"  href="javascript:void(0);"id="del_<%=user.getId()%>"  onclick="ConfirmDelete(this.id)">删除</a>
</td>
      <%}%>
  </tr >
</table>
<a href="InsertUser.jsp?">注册用户</a>
</body>
</html>
